// pages/my_home/my_home.js
let md5 = require("../../utils/md5.js")
let axios = require("../../utils/util.js")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    personInfo: {}, //我的
  },
  homeIndex() {
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let _token =
      "userId=" +
      app.globalData.user_id +
      "&token=" +
      app.globalData.userToken +
      "&time=" +
      timestamp +
      "&memberId=" +
      app.globalData.user_id +
      "&wPQvCAOU$lbIyg9o2wVg0Dt1k^zy&SaX";
    let params = {
      userId: app.globalData.user_id,
      token: app.globalData.userToken,
      memberId: app.globalData.user_id,
      time: timestamp,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/index/homepage",
      data: params
    }).then((res) => {
      // console.log(res.data.result)
      this.setData({
        personInfo: res.data.result
      })
    })
  },
  copy() {
    wx.setClipboardData({
      data: '' + this.data.personInfo.perfect_number,
    })
  },
  //我的房间
  toroom() {

    this.myRoomId()
  },
  //进入我的房间
  myRoomId() {
    let params = {
      userId: app.globalData.user_id,
      token: app.globalData.userToken,
    };
    axios.axios({
      url: "/chat/myRoomId",
      data: params
    }).then((res) => {
      if (res.data.status == '0') {
        this.joinRoom(res.data.result.room_id)
      } else {
        wx.showToast({
          title: res.data.text,
          icon: 'none',
          duration: 2000 //持续的时间
        })
      }
    })
  },
  //我的砖石
  todiamonddetail() {
    wx.showToast({
      title: '小程序暂不支持',
      icon: 'none',
      duration: 2000
    })
    // wx.navigateTo({
    //   url: '/pages/recharge/recharge',
    // })

    // wx.navigateTo({
    //   url: '../link/link?url=' + "http://testapi2.xunyinjiaoyou.com/h5/#/ValentinesDay"
    // })

  },
  //我的设置
  tomeans() {
    wx.navigateTo({
      url: '/pages/means/means',
    })
  },
  collectRoom() {
    wx.navigateTo({
      url: '/pages/fans/Collection',
    })
  },
  tofonst(data) {
    let index = data.currentTarget.dataset.index
    if (index < 2) {
      wx.navigateTo({
        url: '/pages/fans/fans?fanstopNum=' + index,
      })
    } else {
      wx.navigateTo({
        url: '/pages/fans/visitor?fanstopNum=' + index,
      })
    }
  },

  //我的积分
  charmdetail() {
    wx.showToast({
      title: '小程序暂不支持',
      icon: 'none',
      duration: 2000
    })
    // wx.navigateTo({
    //   url: '/pages/wallet/wallet',
    // })
  },
  //设置中心
  tosetFocus() {
    wx.navigateTo({
      url: '/pages/setFocus/setFocus',
    })
  },
  //关于我们
  toabout() {
    // wx.navigateTo({ 
    //   url: '/pages/about/about',
    // })
    wx.navigateTo({
      url: '../link/link?url=' + "https://h5.xunyinjiaoyou.com/h5/#/about"
    })
  },
  //我的装扮
  to_h5() {
    wx.navigateTo({
      url: '../link/link?url=' + "https://h5.xunyinjiaoyou.com/h5/#/myBackpack"
      // url: '../link/link?url=' + "http://testapi2.xunyinjiaoyou.com/h5/#/myBackpack"
    })
  },
  //装扮xx
  to_h5_dress_new() {
    wx.navigateTo({
      url: '../link/link?url=' + "https://h5.xunyinjiaoyou.com/h5/#/dress_new"
      // url: '../link/link?url=' + "http://testapi2.xunyinjiaoyou.com/h5/#/dress_new"
    })
  },
  //官方认证
  to_h5_OfficialCertification() {
    wx.navigateTo({
      url: '../link/link?url=' + "https://h5.xunyinjiaoyou.com/h5/#/OfficialCertification"
      // url: '../link/link?url=' + axios.testInterface + "/h5/#/OfficialCertification"
    })
  },
  //福利中心
  to_h5_ObtainShells() {
    wx.navigateTo({
      url: '../link/link?url=' + "https://h5.xunyinjiaoyou.com/h5/#/ObtainShells"
      // url: '../link/link?url='  + "http://192.168.1.152:8089/#/ObtainShells"
    })
  },
  //我的等级
  to_h5_level() {
    console.log(axios.testInterface);
    wx.navigateTo({
      url: '../link/link?url=' + "https://h5.xunyinjiaoyou.com/h5/#/level_new"
      // url: '../link/link?url=' + axios.testInterface + "/h5/#/level_new"
    })
  },
  to_h5_Notice() {
    wx.navigateTo({
      url: '../link/link?url=' + "https://h5.xunyinjiaoyou.com/h5/#/Notice"
      // url: '../link/link?url='+"http://192.168.1.153:8080/#/Notice"
    })
  },
  toshezhi() {
    console.log('客服中心');
    wx.navigateTo({
      url: '/pages/messageInfo/messageInfo?customerService=' + '10001',
    })
  },
  toperson() {
    wx.navigateTo({
      url: '/pages/me/me?memberId=' + app.globalData.user_id,
    })
  },
  //我的相册
  tomyalbum() {
    wx.navigateTo({
      url: '/pages/myalbum/myalbum',
    })
  },
  //加入房间
  joinRoom(roomId) {
    let params = {
      roomId: roomId,
      userId: app.globalData.user_id.toString(),
      token: app.globalData.userToken,
      shumeiDeviceId: wx.getStorageSync('shumeiDeviceId'),
      clientId: wx.getStorageSync('getClientId')
    };
    axios.axios({
      url: "/chat/joinRoom",
      data: params
    }).then((res) => {
      if (res.data.status != 0) { //加入房间是否在黑名单 
        wx.showToast({
          title: res.data.text,
          icon: 'none',
          duration: 2000 //持续的时间
        })
      } else {
        wx.navigateTo({
          url: '/pages/room/room?roomId=' + roomId,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let user_list = wx.getStorageSync('UserInformation')
    if (user_list) {
      app.globalData.userInfo = user_list
      app.globalData.user_id = user_list.userId;
      app.globalData.userToken = user_list.token;
      app.globalData.perfectNumber = user_list.perfectNumber;
      this.homeIndex()
    } else {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
    // app.user_info().then((res) => {
    //   this.homeIndex()
    // }, (error) => {
    //   console.log('数据请求失败：' + error)
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 3
      })
    }
    this.homeIndex()
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#ffffff'
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.homeIndex()
    var t;
    let _ = this;
    clearTimeout(t);
    t = setTimeout(function () {
      wx.stopPullDownRefresh(); //停止当前页面下拉刷新
      return wx.showToast({
        title: "刷新成功",
        icon: 'none',
        duration: 2000
      })
    }, 1000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
